package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.TTSController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/5/16.
 */

public class MyLocationActivity extends Activity implements AMapNaviListener, AMapNaviViewListener {
    private static final String TAG = "TAG";
    //         AMapNaviView
    private AMapNavi mAMapNavi;
    private AMapNaviView mAMapNaviView;


    private TTSController mTtsManager;
    private NaviLatLng start;
    private NaviLatLng end;
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    private String orderid;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
//View view;
//  view.postDelayed()
//  view.removeCallbacks()
        /**
         * ====================
         */
        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.setAMapNaviViewListener(this);
//       mAMapNavi.moveCamera();
        mAMapNaviView.onCreate(savedInstanceState);
//    SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "=" + appId);


        //获取路径规划
        Intent intent = getIntent();
        start = intent.getParcelableExtra("start");
        end = intent.getParcelableExtra("end");
        orderid = intent.getStringExtra("orderid");
        //获取AMapNavi实例
        mAMapNavi = AMapNavi.getInstance(MyLocationActivity.this.getApplicationContext());
        //添加监听回调，用于处理算路成功
        mAMapNavi.addAMapNaviListener(MyLocationActivity.this);

//讯飞语音集成
        mTtsManager = TTSController.getInstance(getApplicationContext());
        mTtsManager.init();
        mAMapNavi.addAMapNaviListener(mTtsManager);

    }

    @Override
    public void onInitNaviFailure() {
        Log.d(TAG, "onInitNaviFailure: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
//        mAMapNavi.resumeNavi();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        mLocationClient.onpa
        mAMapNaviView.onPause();
//        mAMapNavi.pauseNavi();
        //        仅仅是停止你当前在说的这句话，一会到新的路口还是会再说的
        mTtsManager.stopSpeaking();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
        mAMapNavi.stopNavi();
//        mAMapNavi.removeAMapNaviListener(mTtsManager);
//        mTtsManager.destroy();

        mAMapNavi.destroy();
        mTtsManager.destroy();

    }

    @Override
    public void onInitNaviSuccess() {
        /**
         * 方法:
         *   int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute);
         * 参数:
         * @congestion 躲避拥堵
         * @avoidhightspeed 不走高速
         * @cost 避免收费
         * @hightspeed 高速优先
         * @multipleroute 多路径
         *
         * 说明:
         *      以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，如果为true则此策略会算出多条路线。
         * 注意:
         *      不走高速与高速优先不能同时为true
         *      高速优先与避免收费不能同时为true
         */
        Toast.makeText(this, "线路规划开始", Toast.LENGTH_LONG).show();
        int strategy = 0;
        try {
            strategy = mAMapNavi.strategyConvert(true, false, false, true, false);
//            120.172127,36.019645
            ArrayList<NaviLatLng> sList = new ArrayList<>();

            //120.312643,36.064932
//            NaviLatLng naviLatLng = new NaviLatLng(120.312643, 36.064932);
//            sList.add(naviLatLng);
//            ArrayList<NaviLatLng> eList = new ArrayList<>();
//            NaviLatLng naviLatLng1 = new NaviLatLng(120.172127, 36.019645);
//            eList.add(naviLatLng1);
            //起点终点
            NaviLatLng mNaviStart = start;
            NaviLatLng mNaviEnd = end;
            //起点终点列表
            ArrayList<NaviLatLng> mStartPoints = new ArrayList<NaviLatLng>();
            ArrayList<NaviLatLng> mEndPoints = new ArrayList<NaviLatLng>();
            mEndPoints.add(mNaviEnd);
            mStartPoints.add(mNaviStart);
            ArrayList<NaviLatLng> eList2 = new ArrayList<>();
            boolean b = mAMapNavi.calculateDriveRoute(mStartPoints, mEndPoints, eList2, strategy);
            Log.e(TAG, "onInitNaviSuccess: " + b);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    private long time = 0;

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {
        Log.e(TAG, "onLocationChange: " + aMapNaviLocation.getCoord().getLatitude());
        Map<String, String> headerParams = RequestParam.getHeaderParams();
        long l = System.currentTimeMillis();
        long l1 = l - time;
        time = l;
        if (l1 >= 60000) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("longitude", aMapNaviLocation.getCoord().getLongitude() + "");
            objectObjectHashMap.put("latitude", aMapNaviLocation.getCoord().getLatitude() + "");
            Okhttputils.Instanse(MyLocationActivity.this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/orderhandle/carll", objectObjectHashMap, new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {

                }

                @Override
                public void error(Call call, int code) {

                }

                @Override
                public void suseff(Call call, Response response, Object o) {
                    BaseEntity baseEntity = (BaseEntity) o;
                }
            }, BaseEntity.class);
        }


    }

    @Override
    public void onGetNavigationText(int i, String s) {
        //在这里使用科大讯飞进行导航
//     mTts.startSpeaking(s,mTtsListener);
    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteSuccess() {
        //规划成功以后的回调
//        Toast.makeText(this,"线路规划成功",Toast.LENGTH_LONG).show();
        mAMapNavi.startNavi(NaviType.GPS);
    }

    @Override
    public void onCalculateRouteFailure(int i) {
//        PathPlanningErrCode.ERROR_BUF
        Log.e(TAG, "onCalculateRouteFailure: 失败" + i);
    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {
        //多策略
        Toast.makeText(this, "多路径", Toast.LENGTH_LONG).show();
    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }

    @Override
    public void onNaviSetting() {

    }

    @Override
    public void onNaviCancel() {

    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {

    }

    @Override
    public void onNaviViewLoaded() {

    }

}
